package alphas;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NGramAlpha {
	
	private int n = 3;
	
	public void setN(int n) {
		this.n = n;
	}
	
	public void compare_sentence2(String s1, String s2) {		// not in use
		System.out.println("Comparing two sentenses: ");
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		String[] words = s1.split(" ");
		List<String> w = new ArrayList<>();
		
		System.out.println(words);
		for (int i = 0; i < words.length; ++i) {
			if (words[i].length() >= n) {
				// We can split this word
				System.out.println(words[i] + " =>");
				// Split each word to n-gram
				for (int j = 0; j+3 <= words[i].length(); ++j) {
					String ngram = words[i].substring(j, j+3);
					System.out.print("\t" + ngram);
					
					int count = 0;
					
					Pattern pattern = Pattern.compile(ngram);
					Matcher matcher = pattern.matcher(s2);
					
					while (matcher.find()) {
						count++;
					}
					
					System.out.println(" " + count);
					
				}
			}
		}	
	}
	
	public void compare_sentence(String s1, String s2) {
		String[] w1 = s1.split(" ");
		String[] w2 = s2.split(" ");
		
		double sume = 0.0;
		double max = 0.0;
		
		for (int i = 0; i < w1.length; ++i) {
			max = 0.0;
			for (int j = 0; j < w2.length; ++j) {
				double a = sim(w1[i], w2[j]);
				if (a > max) {
					max = a;
				}
				System.out.println(w1[i] + " & " + w2[j] + " => " + a);
			}
			sume += max;
		}
		
		System.out.println(sume + " " + sume / w1.length);
	}
	
	private double sim(String s1, String s2) {
		double n = 0;
		double counter = 0;
		
		String longer = "";
		String shorter = "";
		
		// We need to chose longer word as this one from which substrings gonna be taken
		if (s1.length() > s2.length()) {
			longer = s1;
			shorter = s2;
		} else {
			longer = s2;
			shorter = s1;
		}
		
		n = longer.length() - 2;
		
		for (int i = 0; i+3 <= longer.length(); ++i) {
			String ngram = longer.substring(i, i+3);
			
			if (shorter.contains(ngram)) {
				counter++;
			}
		}
		
		return counter / n;
	}

}
